﻿using Uber.WebUI.Models;

namespace Uber.WebUI.Controllers
{
    using System.Web.Mvc;
    using Core.Model;
    using Core.Service;

    [Authorize(Roles = "pro, seeker")]
    public class PayRequestController : Controller
    {
        private readonly IPayRequests payRequests;

        public PayRequestController(IPayRequests payRequests)
        {
            this.payRequests = payRequests;
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(PayRequestInput input)
        {
            while (true)
            {
                if (!ModelState.IsValid) break;

                if (!input.Email.Equals(input.ConfirmEmail)) ModelState.AddModelError("ConfirmEmail", "Email don't match");

                if (!ModelState.IsValid) break;

                var r = payRequests.Insert(input.Amount, User.Identity.Name, input.Email, Gl.UserRole());

                if (r != The.Success)
                {
                    ModelState.AddModelError("_FORM", r);
                    break;
                }

                return View("Added");
            }
            return View(input);
        }

        public ActionResult Index(int? page)
        {
            return View(payRequests.GetPageByPlayer(page ?? 1, 5, User.Identity.Name));
        }
    }
}